//  @ Project : GagalPanen
//  @ File Name : dropboxes.cpp
//  @ Date : 3/9/2011
//  @ Author : Denis
//
//

#include <assert.h>
#include "dropboxes.hpp"
#include "fieldconstants.hpp"
#include "gamemaps.hpp"

DropBoxes::DropBoxes(GameInterface* game) :
    game_(game), drop_boxes_() {
  for (int i = 0; i < FieldConstants::getDropBoxesCount(); ++i) {
    int x = FieldConstants::getDropBoxXCoordinate(i);
    int y = FieldConstants::getDropBoxYCoordinate(i);
    assert(x >= 0 && x < FieldConstants::getColumnCount());
    assert(y >= 0 && y < FieldConstants::getRowCount());
    drop_boxes_.push_back(new DropBox(
            game_,
            game_->getMaps()->getField()->getCellAt(x,y)));
  }
}

DropBoxes::DropBoxes(GameInterface* game, std::ifstream& is) :
    game_(game), drop_boxes_() {
  int n;
  is >> n;
  for (int i = 0; i < n; ++i) {
    drop_boxes_.push_back(new DropBox(game, is));
  }
}

void DropBoxes::save(std::ofstream& os) {
  os << drop_boxes_.size() << std::endl;
  for (int i = 0; i < (int)drop_boxes_.size(); ++i) {
    drop_boxes_[i]->save(os);
  }
}

DropBoxes::~DropBoxes() {
  for (int i = 0; i < (int)drop_boxes_.size(); ++i) {
    delete drop_boxes_[i];
  }
}

void DropBoxes::advanceDay() {
  for (int i = 0; i < (int)drop_boxes_.size(); ++i) {
    drop_boxes_[i]->advanceDay();
  }
  return;
}

void DropBoxes::advanceTime() {
  for (int i = 0; i < (int)drop_boxes_.size(); ++i) {
    drop_boxes_[i]->advanceTime();
  }
}

bool DropBoxes::containsDropBoxAt(Cell* cell) {
  for (int i = 0; i < (int)drop_boxes_.size(); ++i) {
    if (drop_boxes_[i]->getCell() == cell) {
      return true;
    }
  }
  return false;
}

DropBox* DropBoxes::getDropBoxAt(Cell* cell) {
  assert(containsDropBoxAt(cell));
  for (int i = 0; i < (int)drop_boxes_.size(); ++i) {
    if (drop_boxes_[i]->getCell() == cell) {
      return drop_boxes_[i];
    }
  }
  assert(false);
}

bool DropBoxes::isSteppable() {
  return false;
}

